package com.surprise.es.service;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.stereotype.Service;

/**
 * ESQueryService.java
 * @author nick
 * 2015年3月11日
 */
@Service
public class ESQueryService {
    public SearchResponse search(Client client) {
        SearchResponse response = client.prepareSearch("hui")
                .setTypes("item")
                .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
                .setQuery(QueryBuilders.matchQuery("sub_title", "副标题1"))
                .setExplain(true)
                .execute().actionGet();
        return response;
    }
    
    public SearchResponse localSearch(Client client) {
        SearchResponse response = client.prepareSearch("hui")
                .setTypes("item")
                .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
//                .setQuery(QueryBuilders.matchQuery("sub_title", "副标题1"))
                .setPostFilter(FilterBuilders.rangeFilter("item_status").gte(1))
                .setFrom(0).setSize(20).setExplain(true)
                .execute().actionGet();
        return response;
    }
    
    public SearchResponse idsQuery(Client client, String... ids) {
        SearchResponse response = client.prepareSearch("hui")
                .setQuery(QueryBuilders.idsQuery("item").ids(ids))
                .setExplain(true)
                .execute()
                .actionGet();
        return response;
    }
}
